package com.stox.ui.javafx.charting.strategy;

import java.util.List;

import javafx.scene.paint.Color;

import org.springframework.stereotype.Component;

import com.stox.dto.preference.PreferenceFactory;
import com.stox.ui.javafx.charting.model.ChartingPreference;
import com.stox.ui.javafx.charting.model.plot.AbstractPlot;
import com.stox.ui.javafx.charting.model.uiunit.AbstractUiUnit;
import com.stox.ui.javafx.charting.widget.PrimaryChart;

@Component
@SuppressWarnings("rawtypes")
public class PlotIndexUiUnitColorStrategy<T extends AbstractUiUnit> implements Strategy<T> {

	@Override
	public void apply(T uiUnit) {
		
		AbstractPlot plot = uiUnit.getPlot();
		if(null == plot.getColor()){
			Color[] colorArray = PreferenceFactory.get(ChartingPreference.class).getColorArray();
			if(plot.getChart() instanceof PrimaryChart){
				List plots = plot.getChart().getPlots();
				plot.setColor(colorArray[plots.indexOf(plot) + 1]);
			}else{
				AbstractPlot frontPlot = plot.getChart().getFrontPlot();
				if(plot == frontPlot){
					plot.setColor(colorArray[0]);
				}else{
					List plots = plot.getChart().getPlots();
					plot.setColor(colorArray[plots.indexOf(plot) + 1]);
				}
			}
		}
		uiUnit.setColor(plot.getColor());
	}

}
